package com.zhangxx.inventory.service.Impl;

import com.google.common.base.Joiner;
import com.google.common.collect.Maps;
import com.zhangxx.common.exception.ExceptionCast;
import com.zhangxx.common.redis.RedisUtil;
import com.zhangxx.common.response.resultCode.CommonCode;
import com.zhangxx.common.util.date.LocalDateUtils;
import com.zhangxx.inventory.mappers.ProductInventoryMapper;
import com.zhangxx.inventory.pojo.ProductInventory;
import com.zhangxx.inventory.service.ProductInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @ClassName ProductInventoryServiceImpl
 * @Description 库存服务实体类
 * @Author leizhang
 * Date 2022/3/18 15:45
 * @Version 1.0
 **/
@Service
public class ProductInventoryServiceImpl implements ProductInventoryService {
    @Resource
    private ProductInventoryMapper productInventoryMapper;
    @Autowired
    private RedisUtil redisUtil;

    private static final Joiner JOINER = Joiner.on("_").skipNulls();

    /**
     * @param productInventory
     * @return com.zhangxx.inventory.pojo.ProductInventory
     * @Description : 库存信息查询
     * @Author : leizhang
     * @Date 15:50 2022/3/18
     **/
    @Override
    public ProductInventory findProductInventory(ProductInventory productInventory) {
        if (null == productInventory.getProductId()) {
            ExceptionCast.cast(CommonCode.INVALIDPARAM);
        }
        return productInventoryMapper.findProductInventory(productInventory);
    }

    /**
     * @param productInventory
     * @return void
     * @Description : 设置redis中的商品缓存信息 redis 中的数据结构
     * {"wareHouseCode_stockCode":{"productId_availableCount":count,"productId_frozenCount":count,"product_processTime":time}}
     * @Author : leizhang
     * @Date 15:58 2022/3/18
     **/
    @Override
    public void setProductInventoryCache(ProductInventory productInventory) {
        if (null == productInventory || null == productInventory.getWarehousCode() || null == productInventory.getProductId() || null == productInventory.getStockCode()) {
            ExceptionCast.cast(CommonCode.INVALIDPARAM);
        }
        String key = JOINER.join(productInventory.getWarehousCode(),productInventory.getStockCode());
        Integer productId = productInventory.getProductId();
        Map<String, Object> valueMap = Maps.newHashMap();
        valueMap.put(JOINER.join(productId, "availableCount"), productInventory.getAvailableCount());
        valueMap.put(JOINER.join(productId, "frozenCount"), productInventory.getFrozenCount());
        valueMap.put(JOINER.join(productId, "processTime"), LocalDateUtils.getNowOfMillion());
        redisUtil.hmset(key, valueMap);
    }

    /**
     * @param productInventory
     * @return void
     * @Description : 删除redis中的库存信息
     * @Author : leizhang
     * @Date 16:23 2022/3/18
     **/
    @Override
    public void removeProductInventoryCache(ProductInventory productInventory) {
        String key = JOINER.join(productInventory.getWarehousCode(),productInventory.getStockCode());
        Integer productId = productInventory.getProductId();
        String availableCountItem = JOINER.join(productId,"availableCount");
        String frozenCountItem = JOINER.join(productId,"frozenCount");
        String processTimeItem = JOINER.join(productId,"processTime");
        redisUtil.hdel(key,availableCountItem,frozenCountItem,processTimeItem);
    }

    /**
     * @param productInventory
     * @return void
     * @Description : 更新库存表
     * @Author : leizhang
     * @Date 16:27 2022/3/18
     **/
    @Override
    public void updateProductInventory(ProductInventory productInventory) {
        if (null == productInventory) {
            ExceptionCast.cast(CommonCode.INVALIDPARAM);
        }
        productInventoryMapper.updateProductInventory(productInventory);
    }

    /**
     * @param productInventory
     * @return com.zhangxx.inventory.pojo.ProductInventory
     * @Description : 读取缓存中的数据
     * @Author : leizhang
     * @Date 20:34 2022/3/18
     **/
    @Override
    public ProductInventory findProductInventoryOfCache(ProductInventory productInventory) {
        if (null == productInventory || null == productInventory.getWarehousCode() || null == productInventory.getProductId() || null == productInventory.getStockCode()) {
            ExceptionCast.cast(CommonCode.INVALIDPARAM);
        }
        String key = JOINER.join(productInventory.getWarehousCode(),productInventory.getStockCode());
        String item = JOINER.join(productInventory.getProductId(),"availableCount");
        Double availableCount = (Double) redisUtil.hget(key, item);
        if (null != availableCount) {
            productInventory.setAvailableCount(availableCount);
        }else{
            productInventory = null;
        }
        return productInventory;
    }
}
